package edu.xidian.onlinedocument.views.elder;

import com.fasterxml.jackson.annotation.JsonInclude;
import edu.xidian.onlinedocument.entity.DocumentData;
import edu.xidian.onlinedocument.entity.UserData;

import edu.xidian.onlinedocument.entity.UserTokenData;
import edu.xidian.onlinedocument.views.groups.DataGroupA;
import edu.xidian.onlinedocument.views.groups.DataGroupB;
import edu.xidian.onlinedocument.views.groups.DataGroupC;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.List;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
@NoArgsConstructor
public class UserDataMapper {
    @NotBlank(message = "用户昵称不能为空", groups = DataGroupA.class)
    @Size(min = 4, max = 20, message = "用户昵称的长度在4到20个字符之间", groups = DataGroupA.class)
    private String userName;

    @NotBlank(message = "用户账号不能为空", groups = DataGroupB.class)
    @Pattern(regexp = "^([a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+)|(1[0-9]\\d{9})$",
            message = "账号只能是邮箱或11位手机号", groups = DataGroupB.class)
    private String userNum;

    @NotBlank(message = "用户密码不能为空", groups = DataGroupB.class)
    @Size(min = 6, max = 18, message = "密码太短或太长", groups = DataGroupC.class)
    private String userKey;

    private UserTokenData userTokenData;

    private List<DocumentData> documentData;

    public UserDataMapper(UserData userData){
        this.userName = userData.getUserName();
        this.userNum = userData.getUserNum();
        this.userKey = userData.getUserKey();
        this.userTokenData = userData.getUserTokenData();
        this.documentData = userData.getCreateDocumentData();
    }

    public static UserDataMapper turn(UserData userData){
        return new UserDataMapper(userData);
    }
}
